package com.sc.outpatientsys.controller;

import com.sc.outpatientsys.entity.Student;
import com.sc.outpatientsys.service.IStudentService;
import com.sc.outpatientsys.service.ex.InsertException;
import com.sc.outpatientsys.service.ex.UserNameHasExistException;
import com.sc.outpatientsys.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController extends BaseController{
    @Autowired
    private IStudentService studentService;

    @RequestMapping("/reg")
    public JsonResult<Void> reg(Student student){
        studentService.reg(student);
        return new JsonResult<>(OK);

    }
    @RequestMapping("/login")
    public JsonResult<Student> login(String userName, String password, HttpSession session){
        Student data = studentService.login(userName,password);
        session.setAttribute("id",data.getSt_ID());
        session.setAttribute("username",data.getUserName());
        return new JsonResult<Student>(OK,data);
    }
    @RequestMapping("/findByID")
    public JsonResult<Student> findByID(int st_ID){
        Student data = studentService.findByID(st_ID);
        return new JsonResult<Student>(OK,data);
    }
    @RequestMapping("/findByName")
    public JsonResult<Student> findByName(String userName){
        Student data = studentService.findByName(userName);
        return new JsonResult<Student>(OK,data);
    }

    @RequestMapping("/findAll")
    public JsonResult<List<Student>> findALl(){
        List<Student> data = studentService.findAll();
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/update")
    public JsonResult<Void> update(Student student){
        studentService.update(student);
        return new JsonResult<>(OK);
    }
    @RequestMapping("/delete")
    public JsonResult<Void> delete(int st_ID){
        studentService.delete(st_ID);
        return new JsonResult<>(OK);
    }
}
